/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or  
 *	implied, as to its fitness for any particular use.
 *
 *	AppController
 *
 *	Inherits From:		NSObject
 *
 *	Conforms To:		None
 *
 *	Declared In:		AppController.h
 *
 *------------------------------------------------------------------------*/
#import <foundation/NSString.h>

#import "AppController.h"
#import "ScrollViewExtensions.h"
#import "Catalog.h"
#import <ansi/ctype.h>
#include <ansi/time.h>


#define SLEEP(sleeptime) \
		do { \
			struct timeval tv; \
			tv.tv_sec = sleeptime; tv.tv_usec = 0; \
		    select(0, NULL, NULL, NULL, &tv); \
		} while(0)




@implementation AppController

/*--------------------------------------------------------------------------
 *	Application delegate methods
 *------------------------------------------------------------------------*/
- appDidInit: sender
{
	id	entity = [(EODatabaseDataSource *)[controller dataSource] entity];
	id	qualifier = [[EOQualifier allocWithZone: [self zone]]
			initWithEntity:entity qualifierFormat:@"au_fname = \"Akiko\""];

	[window disableDisplay];
	startup = YES;

	[textDisplay setVertScrollerRequired:NO];
	[[textDisplay docView] setRetainedWhileDrawing:YES];

	/*----------------------------------------------------------------------
	 *	turn buffering on so changes never affect database
	 *--------------------------------------------------------------------*/
	[controller setSavesToDataSourceAutomatically:NO];
	[controller setSavesToObjectsAutomatically:YES];

	[[[(EODatabaseDataSource *)[controller dataSource] databaseChannel]
		adaptorChannel] setDebugEnabled:YES];

	[(EODatabaseDataSource *)[controller dataSource] setQualifier:qualifier];
	[controller fetch];

/*--------------------------------------------------------------------------
 *	This was included in alpha to select a single record -- apparently, the
 *	associations are smart enough now to display the first selected record.
 *
 *	[controller setSelectionIndexes: selectionArray];
 *	[[controller associations] makeObjectsPerform:
 *		@selector(selectionDidChange)];
 *	[self updateObjectView:[[controller allObjects] objectAtIndex:0]];
 *------------------------------------------------------------------------*/

	[[textDisplay docView] setText:""];

	[window reenableDisplay];
	[window display];
	[window makeKeyAndOrderFront:nil];

	startup = NO;
	return self;
}


/*--------------------------------------------------------------------------
 *	Printing Trace Information
 *------------------------------------------------------------------------*/
- console: (NSArray *) argArray
{
	BOOL	flag;

	if (startup == YES) return self;

	[[argArray objectAtIndex:1] getValue: &flag];
	if ([[traceSetting cellAt:(int)flag:0] state] == NO)
		return self;

	[console sprintf: "%s", [[argArray objectAtIndex:0] cString]];

	return self;
}


- display: (NSArray *) argArray
{
	BOOL	flag;

	if (startup == YES) return self;

	[[argArray objectAtIndex:1] getValue: &flag];
	if ([[traceSetting cellAt:(int)flag:0] state] == NO)
		return self;

	[[textDisplay docView] setText: [[argArray objectAtIndex:0] cString]];

	NXPing();
	if ([traceDelay intValue])
		SLEEP([traceDelay intValue]);


	[[textDisplay docView] setText: ""];

	return self;
}


/*--------------------------------------------------------------------------
 *	Update object view
 *------------------------------------------------------------------------*/
- updateObjectView: newObject
{
	BOOL temp = startup;

	startup = YES;
	[objectName setStringValue: [[newObject authorName] cString]];
	[objectPrice setStringValue:
			[[(NSNumber *)[newObject price] description] cString]];
	[objectTitle setStringValue: [[newObject volumeTitle] cString]];

	startup = temp;
	return self;
}

@end
